package com.emmmya.ocs.common.converter;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import org.thymeleaf.util.StringUtils;

import javax.persistence.AttributeConverter;

public class JpaConverterListJson  implements AttributeConverter<Object, String> {
    @Override
    public String convertToDatabaseColumn(Object o) {
        if(o == null){
            return null;
        }
        return JSON.toJSONString(o);
    }

    @Override
    public Object convertToEntityAttribute(String s) {
        if(StringUtils.isEmpty(s)){
            return null;
        }
        JSONArray objects = JSON.parseArray(s);
        return objects;
    }
}